package com.liuyanban.action.forum;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.liuyanban.action.base.BaseAction;
import com.liuyanban.annotation.PrivilegeInfo;
import com.liuyanban.domain.forum.Forum;
import com.liuyanban.domain.forum.ForumVO;
import com.liuyanban.domain.forum.Forumliuyan;
import com.liuyanban.domain.forum.ForumliuyanVO;
import com.liuyanban.domain.user.User;
import com.liuyanban.query.PageResult;
import com.liuyanban.query.forum.ForumQuery;
import com.liuyanban.query.forum.ForumliuyanQuery;
import com.liuyanban.service.forum.ForumService;
import com.liuyanban.service.forum.ForumliuyanService;
import com.liuyanban.service.user.UserService;
import com.liuyanban.utils.DateUtil;
import com.opensymphony.xwork2.ActionContext;

@Controller("forumAction")
@Scope("prototype")
public class ForumAction extends BaseAction<Forum> {
	@Resource(name = "userService")
	private UserService userService;

	@Resource(name = "forumService")
	private ForumService forumService;

	@Resource(name = "forumliuyanService")
	private ForumliuyanService forumliuyanService;

	private ForumQuery forumQuery = new ForumQuery();
	private ForumliuyanQuery forumliuyanQuery = new ForumliuyanQuery();

	public String add() {
		Forum forum = new Forum();
		User user = (User) ActionContext.getContext().getSession().get("user");
		// 封装帖子内容和标题
		BeanUtils.copyProperties(this.getModel(), forum);
		// 添加发表时间
		forum.setPublishtime(DateUtil.formatDateByFormat(new Date(),
				DateUtil.DATETIME_FORMAT));
		forum.setUserid(user.getUserid());
		this.forumService.saveEntry(forum);
		return action2action;
	}

	public String showPageResult() {
		List<ForumVO> forumVOlist = new ArrayList<ForumVO>();

		forumQuery.setCurrentPage(this.getCurrentPage());
		forumQuery.setKeyValues(null);
		PageResult<Forum> forums = this.forumService.getPageResult(forumQuery);

		List<Forum> forumlist = forums.getRows();

		for (Forum forum : forumlist) {
			ForumVO forumVO = new ForumVO();
			forumVO.setForumid(forum.getForumid());
			forumVO.setPublishcontent(forum.getPublishcontent());
			forumVO.setPublishtime(forum.getPublishtime());
			forumVO.setUsername(this.userService
					.getEntryById(forum.getUserid()).getUsername());
			forumVO.setTitle(forum.getTitle());

			forumliuyanQuery.setForumid(forum.getForumid());
			List<Forumliuyan> forumliuyans = this.forumliuyanService
					.getEntriesByCondition(forumliuyanQuery);

			List<ForumliuyanVO> forumliuyanVOs = new ArrayList<ForumliuyanVO>();
			for (Forumliuyan forumliuyan : forumliuyans) {
				ForumliuyanVO forumliuyanVO = new ForumliuyanVO();
				forumliuyanVO.setReplycontent(forumliuyan.getLiuyancontent());
				forumliuyanVO.setUsername(this.userService.getEntryById(
						forumliuyan.getUserid()).getUsername());
				forumliuyanVOs.add(forumliuyanVO);
			}
			forumVO.setForumliuyanvos(forumliuyanVOs);
			forumVOlist.add(forumVO);
		}
		ActionContext.getContext().put("forumVOlist", forumVOlist);
		ActionContext.getContext().put("PageResult", forums);
		return listAction;
	}

	public String showForumPageResult() {
		List<ForumVO> forumVOlist = new ArrayList<ForumVO>();

		forumQuery.setCurrentPage(this.getCurrentPage());
		forumQuery.setPageSize(10);
		forumQuery.setKeyValues(null);

		PageResult<Forum> forums = this.forumService.getPageResult(forumQuery);

		List<Forum> forumlist = forums.getRows();

		for (Forum forum : forumlist) {
			ForumVO forumVO = new ForumVO();
			forumVO.setForumid(forum.getForumid());
			forumVO.setPublishcontent(forum.getPublishcontent());
			forumVO.setPublishtime(forum.getPublishtime());
			forumVO.setUsername(this.userService
					.getEntryById(forum.getUserid()).getUsername());
			forumVO.setTitle(forum.getTitle());

			forumVOlist.add(forumVO);
		}
		ActionContext.getContext().put("forumVOlist", forumVOlist);
		ActionContext.getContext().put("PageResult", forums);
		return showlist;
	}

	@PrivilegeInfo(name = "修改帖子内容")
	public String updateUI() {
		Forum forum = this.forumService.getEntryById(this.getModel()
				.getForumid());
		/**
		 * 查看发表该帖子的用户
		 */
		User loginUser = (User) this.getSession().getAttribute("user");
		if (forum.getUserid().equals(loginUser.getUserid())) {
			this.getSession().setAttribute("auth", "有权限");
		} else {
			this.getSession().setAttribute("auth", "无权限");
		}

		ForumVO forumVO = new ForumVO();
		forumVO.setForumid(forum.getForumid());
		forumVO.setPublishcontent(forum.getPublishcontent());
		forumVO.setPublishtime(forum.getPublishtime());
		forumVO.setUsername(this.userService.getEntryById(forum.getUserid())
				.getUsername());
		forumVO.setTitle(forum.getTitle());
		ActionContext.getContext().put("forum", forumVO);
		return updateUI;
	}

	public String update() {
		Forum forum = this.forumService.getEntryById(this.getModel()
				.getForumid());
		forum.setTitle(this.getModel().getTitle());
		forum.setPublishcontent(this.getModel().getPublishcontent());
		this.forumService.updateEntry(forum);
		return "chain";
	}

	@PrivilegeInfo(name = "删除帖子")
	public String delete() {
		Forum forum = this.forumService.getEntryById(this.getModel()
				.getForumid());
		/**
		 * 查看发表该帖子的用户
		 */
		User loginUser = (User) this.getSession().getAttribute("user");
		if (forum.getUserid().equals(loginUser.getUserid())) {
			this.getSession().setAttribute("auth", "有权限");
		} else {
			this.getSession().setAttribute("auth", "无权限");
		}
		this.forumService.deleteEntryById(this.getModel().getForumid());
		return "chain";
	}
}
